<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<script id="temp_mess" type="text/html">
    <table class="table table-border table-hover">
        <colgroup>
            <col width="5%">
            <col width="15%">
            <col width="70%">
            <col width="10%">
        </colgroup>
        <thead>
        <tr>
            <th>
                <input type="checkbox" id="checkall" onclick="checkedAll(this)">
            </th>
            <th>类型</th>
            <th>内容</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        {{if list.length==0}}
        <tr>
            <td colspan="5">暂未找到您的数据</td>
        </tr>
        {{/if}}
        {{each list as value i}}
        <tr class="active">
            <td><input name="subBox" type="checkbox" value='{{value.guid}}'></td>
            <td><strong class="myMess-type">
                {{if value.isRead=='0'}}
                <img src="<c:url value="/resource/images/new.gif"></c:url>">
                {{/if}}
                {{value.module}}</strong></td>
            <td>{{value.content}}</td>
            <td><a href="javascript:;" class="btn btn-xs btn-red" onclick="delSingle('{{value.guid}}')">删除</a></td>
        </tr>

        {{/each}}

        </tbody>
    </table>


</script>
<div class="ucr-centent user-info marginTop60">
    <h3 class="ucr-title">消息提醒</h3>
    <div class="myMessage-content">
        <div class="myMessage-btn-header">
            <a href="javascript:;" class="btn btn-xs btn-red" onclick="del()">全部删除</a>
            <a href="javascript:;" class="btn btn-xs btn-blue" onclick="allread()">全部设为已读</a>
        </div>
        <div id="divMess">

        </div>
    </div>
</div>

<script>
    var page_index = 1, page_size = 10;
    $(function () {
        $(".uc-nav li").each(function () {
            $(this).click(function () {
                $(".active").removeClass("active");
                $(this).addClass("active");
                getList(page_index, page_size, $(this).attr("status"));
            })
        })
        getList(page_index, page_size);


    });
    function getList(pi, ps) {
        var data = {
            ps: ps,
            pi: pi
        };

        $.ajax({
            url: "<c:url value='/messages/page'/>",
            type: "GET",
            data: data,
            dataType: "json",
            success: function (res) {
                if (res.succ) {
                    var shtml = template("temp_mess", res.data);
                    $("#divMess").html(shtml);
                    $("#page").jqPaginator({
                        totalPages: res.data.totalPage == 0 ? 1 : res.data.totalPage,
                        visiblePages: 10,
                        currentPage: pi,
                        pageSize: ps,
                        onPageChange: function (n, type) {
                            if (type == 'change') {
                                getList(n, ps);
                            }
                        }
                    });
                } else {
                    toast(res.msg);
                }
            },
            error: function (res) {
                toast(res.msg);
            }
        });
    }
    function checkedAll(o) {
        if (o.checked)
            $('input[name="subBox"]').prop("checked", true);
        else
            $('input[name="subBox"]').prop("checked", false);
    }
    function isSel() {
        var cnt = 0;
        var sel = ''
        $('input[name="subBox"]').each(function (index, domEle) {
            if (domEle.checked) {
                cnt = cnt + 1;

                sel = sel + $(domEle).val() + ',';
            }
        });
        if (cnt < 1) {
            layer.open({
                content: '请选择记录！',
                scrollbar: false
            });
            return false;
        }
        return sel;
    }
    function allread() {
        var sel = isSel();
        if (!sel)
            return;
        var data = {
            ids: sel
        };
        $.ajax({
            url: "<c:url value='/messages/read'/>",
            type: "POST",
            data: data,
            dataType: "json",
            success: function (res) {
                if (res.succ) {
                    getList(1, 10);
                } else {
                    toast(res.msg);
                }
            },
            error: function (res) {
                toast(res.msg);
            }
        });

    }

    function del() {


        var sel = isSel();
        if (!sel)
            return;
        var data = {
            ids: sel
        };

        layer.confirm('确定删除么？', {
            btn: ['确定','取消'] //按钮
        }, function(){
            $.ajax({
                url: "<c:url value='/messages/del'/>",
                type: "POST",
                data: data,
                dataType: "json",
                success: function (res) {
                    if (res.succ) {
                        getList(1, 10);
                    } else {
                        toast(res.msg);
                    }
                },
                error: function (res) {
                    toast(res.msg);
                }
            });
        }, function(){

        });


    }

    function delSingle(guid) {
        var data = {
            ids: guid
        };
        layer.confirm('确定删除么？', {
            btn: ['确定','取消'] //按钮
        }, function(index){
            layer.close(index);
            $.ajax({
                url: "<c:url value='/messages/del'/>",
                type: "POST",
                data: data,
                dataType: "json",
                success: function (res) {
                    if (res.succ) {
                        getList(1, 10);
                    } else {
                        toast(res.msg);
                    }
                },
                error: function (res) {
                    toast(res.msg);
                }
            });
        }, function(){

        });

    }

</script>